
import { LevelData } from "../class/levelData"
import { PlayerData } from "../class/playerData"
import { Emitter } from "../utils/emmiter"
import { MessageType } from "../utils/message"
import { Utils } from "../utils/utils"
import DD from "./dynamic_data_manager"


/**
 *
 * @dec 本地存储文件
 */
const { ccclass, property } = cc._decorator

@ccclass
export default class StorageManager extends cc.Component {
    static _instance: StorageManager = null

    static get instance() {
        if (this._instance == null) {
            this._instance = new StorageManager()
        }
        return this._instance
    }
    levelInfo: LevelData = { maxLevel: 1, curLevel: 1, money: 100, chractorId:1,statIds: [], skillLevel: null, characterLevel: null }

    skillLevel = { 3: 1, 5: 1, 7: 1, 11: 1 }

    characterLevel = { 1: 1, 2: 1, 3: 1, 4: 1 }
    charactorTicket = { 1: 5, 2: 5, 3: 5, 4: 5 }

    skillMoneyCost = { 1: 50, 2: 100, 3: 200, 4: 350, 5: 500, 6: 600, 7: 800, 8: 1000, 9: 200, 10: 500 }
    skillLevelInfo = { 1: 1, 3: 1, 5: 1, 11: 1 }

    //配置项
    skillTicketCost = { 1: 10, 2: 50, 3: 100, 4: 200, 5: 300, 6: 400, 7: 50, 8: 100, 9: 200, 10: 500 }
    remainSkillTicket = 10
    equipIds =[]
    levelExpMap = { 1: 10, 2: 25, 3: 50, 4: 100, 5: 150, 6: 200, 7: 250, 8: 300, 9: 400 }
    playerData: PlayerData = { hp: 100, maxHp: 100, level: 1, curExp: 0, bulletCount: 1 }
    skills: string[] = []
    skillSpeicialIds: string[] = []
    canExplode: number = 0
    explosionRadius: number = 1
    spreadCount: number = 0
    carCount: number = 1;
    ballStrenth: number = 0
    carCd: number = 12
    ballCd: number = 8
    ballExistTime: number = 24
    ballCount: number = 1
    ballSpeed: number = 0
    bindEvent() {

    }

    loadDataByKey(key: string) {
        let data = cc.sys.localStorage.getItem(key)
        if (data) {
            return JSON.parse(data)
        } else {
            return null
        }
    }

    saveDataByKey(key: string, data) {
        cc.sys.localStorage.setItem(key, JSON.stringify(data))
    }

    removeDataByKey(key: string) {
        cc.sys.localStorage.removeItem(key)
    }
    /**
     * 加载本地资源
     */
    loadPlayerData() {
        return new Promise((resolve, reject) => {
            let data = this.loadDataByKey('userdata')
            //在这里加载数据
            // HomeManager.instance.buildings = data['B']
            if (data != null) {
                cc.log('村挡数据')

                this.levelInfo = data.level
                if (data.skillLevel != null) {
                    cc.log(data.skillLevel)
                    this.skillLevel = data.skillLevel
                }
                if(data.characterLevel != null){
                    this.characterLevel = data.characterLevel
                }
            }

            //  DD.instance.gender = data['GD']

            resolve(data)
        })
    }
    /**
     * 保存数据
     */
    savePlayerData(name: string = 'userdata') {
        let data = { 'level': this.levelInfo, 'skillLevel': this.skillLevel,'characterLevel':this.characterLevel }
        cc.sys.localStorage.setItem(name, JSON.stringify(data))
        cc.log('数据保存成功:' + name)
        return data
    }

  

    isFristPlay() {
        if (cc.sys.localStorage.getItem('isFirst')) {
            return false
        } else {
            return true
        }
    }



    addExp(num: number) {
        this.playerData.curExp += num;
        if (this.playerData.curExp >= this.levelExpMap[this.playerData.level]) {
            this.playerData.curExp = 0;
            this.playerData.level++;
            Emitter.fire(MessageType.playerLevelUp);
        }
    }
}
